package com.mobapping;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;

public class DataAdapter implements IDataAdapter {
	
	private static final String NAMESPACE =  "http://mobapping.com"; //"http://10.0.2.2/soap";
	
	private static final String pharmacyURL = "http://www.mekya.com/mobapping/server.php";//"http://www.mekya.com/mobapping/test/MobAppingWebService/server.php";  //"http://10.0.2.2/soap/server.php"; //	
	//private static final String pharmacyURL = "http://10.0.2.2/soap/server.php"; //	
	private Context context;
	private FileOperator fileOperator;
	
	public DataAdapter()
	{
		
	}
	
	public DataAdapter(Context context)
	{
		this.context = context;
		fileOperator = new FileOperator(this.context);
	}
	
	@Override
	public void getCameras( final IArrayListDataListener dataListener) {
		Thread thread = new Thread()
		{
			Handler handler = new Handler();
			ArrayList<Object> kameralar = new ArrayList<Object>();
			@Override
			public void run() {
				
	            kameralar.add(new ElementInfo("Kamera1", 1,39.91,32.84));          
	            kameralar.add(new ElementInfo("Kamera2", 4,39.93,32.80));            	
	            kameralar.add(new ElementInfo("Kamera3", 2,39.94,32.83));            	
	            kameralar.add(new ElementInfo("Kamera4",  3,39.95,32.86));            	
	            kameralar.add(new ElementInfo("Kamera5", 1,39.92,32.82));          
	            getPictures(190,3); 
				dataListener.dataArrived(kameralar);
			}
		};
		thread.start();	
		
	}
	
	private ElementInfo getKameraObject(SoapObject object) 
	{
	    try {
	    	return new ElementInfo((String)object.getProperty("Name") ,Integer.parseInt(object.getProperty("id").toString()), Double.parseDouble(object.getProperty("lattitude").toString()), Double.parseDouble(object.getProperty("longitude").toString()));
 
        } catch(Exception E) {
        	return new ElementInfo((String)object.getProperty("Name") ,Integer.parseInt(object.getProperty("id").toString()), Double.parseDouble(object.getProperty("lattitude").toString()), Double.parseDouble(object.getProperty("longitude").toString()));
        	
        }		
	
	}
	
	public void getPictures(int cameraId, int numberOfFrames)
	{
		boolean downloadResult;
		String pictureId;
		int startIndex=0, endIndex;
		String siteUrl = "http://tkm.ibb.gov.tr/data/cam.aspx?c="+cameraId+"&t="+numberOfFrames;
		String pictureUrl;
		String response = getPage(siteUrl);
		endIndex = response.lastIndexOf('=');
		int i = 0;
		while(true)
		{
			startIndex = response.indexOf('=', startIndex+1);
			if(startIndex < endIndex)
			{
				pictureId = response.substring(startIndex+1, startIndex+7);
				pictureUrl = "http://tkm.ibb.gov.tr/endura/"+cameraId+"/"+pictureId+".jpg";
				downloadResult = fileOperator.downloadAndSaveFile(pictureUrl, i + ".jpg" /*picture"+pictureId+".jpg"*/);
				i++;
				
			}
			else {
				break;
			}
		}
		
	}
	
	private String getPage(String siteUrl) {
    	String str = "***";

        try
    	{
    		HttpClient hc = new DefaultHttpClient();
    		HttpPost post = new HttpPost(siteUrl);

    		HttpResponse rp = hc.execute(post);

    		if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    		{
    			str = EntityUtils.toString(rp.getEntity());
    		}
    	}catch(IOException e){
    		e.printStackTrace();
    	}  
    	
    	return str;
    }

	
}
